package com.eastfair.venuebooking.service.impl;

import com.eastfair.common.api.CollectRecordFeign;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.pay.dto.CollectDTO;
import com.eastfair.pay.dto.UnCollectDTO;
import com.eastfair.pay.vo.CollectRecordVO;
import com.eastfair.venuebooking.service.VenueBookingCollectRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 支付系统认款记录 service.
 *
 * @author dqq
 * @date 2022 /6/25
 */
@Service
@Slf4j
public class VenueBookingCollectRecordServiceImpl implements VenueBookingCollectRecordService {

    @Resource
    private CollectRecordFeign collectRecordFeign;

    @Override
    public boolean collect(CollectDTO collectDTO) {
        log.info("collect - 调用支付系统认款, collectDTO={}", collectDTO);

        R<?> result = collectRecordFeign.collect(collectDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return true;
    }

    @Override
    public boolean uncollect(UnCollectDTO unCollectDTO) {
        log.info("uncollect - 调用支付系统取消认款, unCollectDTO={}", unCollectDTO);

        R<?> result = collectRecordFeign.uncollect(unCollectDTO);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return true;
    }

    @Override
    public List<CollectRecordVO> listCollectsByPlanId(Long paymentPlanId) {
        log.info("queryCollectByPlanId - 付款计划id查询认款金额, paymentPlanId={}", paymentPlanId);

        R<List<CollectRecordVO>> result = collectRecordFeign.queryCollectByPlanId(paymentPlanId);
        if (!result.getIsSuccess()) {
            throw BizException.wrap(result.getCode(), result.getMsg());
        }
        return result.getData();
    }
}
